from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from options import options


class Discuz():
    def __init__(self, driver):
        self.driver = driver

    def login(self,username,password):
        self.driver.find_element(By.ID, 'ls_username').clear()
        self.driver.find_element(By.ID, 'ls_username').send_keys(username)
        self.driver.find_element(By.ID, 'ls_password').clear()
        self.driver.find_element(By.ID, 'ls_password').send_keys(password)
        sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button/em').click()
        sleep(3)

    def register(self,newuser,newpwd,newemail):
        # 注册
        self.driver.find_element(By.LINK_TEXT, '立即注册').click()
        sleep(3)
        self.driver.find_element(By.ID, 'y99G29').clear()
        self.driver.find_element(By.ID, 'y99G29').send_keys(newuser)
        self.driver.find_element(By.ID, 'IsW5M7').clear()
        self.driver.find_element(By.ID, 'IsW5M7').send_keys(newpwd)
        self.driver.find_element(By.ID, 'vK663s').clear()
        self.driver.find_element(By.ID, 'vK663s').send_keys(newpwd)
        self.driver.find_element(By.ID, 'LOosck').clear()
        self.driver.find_element(By.ID, 'LOosck').send_keys(newemail)
        sleep(1)
        self.driver.find_element(By.ID, 'registerformsubmit').click()
        sleep(5)

    def read(self,username='admin',password='123456'):
        self.login(username,password)
        # 进入默认板块
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()
        # 打开帖子  copy selector使用层级
        self.driver.find_element(By.CSS_SELECTOR, '#normalthread_805 > tr > th > a.s.xst').click()
        sleep(3)

    def post(self,subject,message,username='admin',password='123456'):
        self.login(username,password)
        # 进入默认板块
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()
        sleep(1)
        # 发帖
        self.driver.find_element(By.ID, 'newspecial').click()
        sleep(1)
        self.driver.find_element(By.ID, 'subject').clear()
        self.driver.find_element(By.ID, 'subject').send_keys(subject)
        # 嵌套页面
        self.driver.switch_to.frame('e_iframe')
        self.driver.find_element(By.XPATH, '/html/body').clear()
        self.driver.find_element(By.XPATH, '/html/body').send_keys(message)
        sleep(1)
        # 跳出嵌套页面
        self.driver.switch_to.default_content()
        self.driver.find_element(By.ID, 'postsubmit').click()
        sleep(3)

    def reply(self,postmessage,username='admin',password='123456'):
        self.login(username,password)
        # 进入默认板块
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()
        # 打开帖子  copy selector使用层级
        self.driver.find_element(By.CSS_SELECTOR, '#normalthread_805 > tr > th > a.s.xst').click()
        sleep(3)
        # 回复帖子
        self.driver.find_element(By.CSS_SELECTOR, '#post_reply > img').click()
        sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="postmessage"]').clear()
        self.driver.find_element(By.XPATH, '//*[@id="postmessage"]').send_keys(postmessage)
        sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, '#postsubmit > span').click()
        sleep(3)

    def logout(self):
        self.login(username, password)
        self.driver.find_element(By.LINK_TEXT, '退出').click()
        sleep(3)


if __name__ == '__main__':
    url = r'http://192.168.183.140/upload/'
    # 登录
    username = 'admin'
    password = '123456'
    # 注册
    # newuser = 'yh2'
    # newpwd = '123456'
    # newemail = 'yh2@qq.com'
    # 发帖
    # subject = '标题1'
    # message = '帖子1'
    # 回帖
    postmessage = '回帖2'
    driver = webdriver.Chrome(options=options)
    driver.get(url)

    discuz = Discuz(driver)
    # discuz.assert(username,password)
    # discuz.register(newuser,newpwd,newemail)
    # discuz.read(username,password)
    # discuz.post(subject, message)
    # discuz.reply(postmessage)
    # discuz.logout()

    driver.quit()
